import {
	ref,
	onMounted,
	nextTick
} from "vue";
export default function useGoodsList(props) {
	const app = getApp()
	const loading = ref(true)
	const loadStatus = ref('loadmore')
	const lists = ref([])
	const page = ref(1)
	let history = {}
	// onReachBottom(() => {
	//    props.pullDown ? getData() : ''
	// })
	const getData = (value) => {
		if (JSON.stringify(history) == JSON.stringify(value) && loadStatus.value == 'nomore') {
			return
		}
		if (value && !value.name) {
			value.name = ""
		}
		history = value
		loading.value = true
		loadStatus.value = 'loading'
		app.axios({
			url: 'api.goods/getGoods',
			data: {
				...value,
				page: page.value
			},
			loading: false
		}).then(res => {
			lists.value = res.page.current == 1 ? res.list : lists.value.concat(res.list)
			if (res.page.current >= res.page.pages) {
				loadStatus.value = 'nomore'
			} else {
				loadStatus.value = 'loadmore'
				page.value = res.page.total <= 10 ? 1 : res.page.current + 1
			}
			loading.value = false
			uni.hideLoading();
		}).catch(error => {
			loading.value = false
		})
	}
	const searchData = (value) => {
		page.value = 1
		history = {}
		getData(value)
	}
	const toDetail = (goods) => {
		app.vibrate()
		uni.navigateTo({
			url: '/pages/goodsDetail/goodsDetail?code=' + goods.code,
		})
	}
	const setListsData = (value) => {
		loading.value = true
		loadStatus.value = 'loading'
		setTimeout(() => {
			lists.value = value
			loading.value = false
			loadStatus.value = 'loadmore'
		}, 1000);
	}
	const onLoadData = (e) => {
		if (props?.defaultRequest) {
			getData(e)
		}
	}
	onMounted(() => {
		onLoadData()
	})
	return {
		loading,
		loadStatus,
		lists,
		page,
		getData,
		setListsData,
		searchData,
		toDetail,
	}
}